package com.easy.dfcapi.web;

import com.easy.common.bean.Result;
import com.easy.common.constant.EnumConfig;
import com.easy.common.util.IPUtil;
import com.easy.core.web.Api20Controller;
import com.easy.dfcapi.res.*;
import com.easy.dfcapi.service.ApiActivityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
/**
 * 活动相关控制器
 */

@Controller
@RequestMapping(value = "${apiPath}/2.0/activity")
public class ApiActivityController extends Api20Controller {


    @Autowired
    private ApiActivityService activityService;

    /**
     * 可报名的活动列表(天天特价/聚爆款/疯抢/拼团/跳尾货)
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ActivityList", method = RequestMethod.POST)
    public Result ActivityList(Response<ActivityRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return activityService.ActivityList(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.DB_ERROR);
            return rt;
        }
    }

    /**
     * 活动报名(天天特价/聚爆款/疯抢/拼团/跳尾货订货会)
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ActivityEnrollAdd", method = RequestMethod.POST)
    public Result ActivityEnrollAdd1(Response<ActivityEnrollRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));

            return activityService.ActivityEnrollAdd(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.DB_ERROR);
            return rt;
        }
    }


    /**
     * 活动报名列表
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ActivityEnrollList1", method = RequestMethod.POST)
    public Result ActivityEnrollList1(Response<ActivityEnrollQueryRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return activityService.ActivityEnrollList(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.DB_ERROR);
            return rt;
        }
    }

    /**
     * 报名撤回
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ActivityEnrollCancel", method = RequestMethod.POST)
    public Result ActivityEnrollCancel(Response<ActivityEnrollQueryRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return activityService.ActivityEnrollCancel(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.DB_ERROR);
            return rt;
        }
    }
    /**
     * 活动报名详情
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ActivityEnrollDetail", method = RequestMethod.POST)
    public Result ActivityEnrollDetail(Response<ActivityEnrollQueryRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));
            return activityService.ActivityEnrollDetail(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.DB_ERROR);
            return rt;
        }
    }
    /**
     * 活动报名付款
     * @param response
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "ActivityEnrollPay", method = RequestMethod.POST)
    public Result ActivityEnrollPay(Response<PayInfoRes> response, HttpServletRequest request) {
        try {
            if(!response.isValidator()){
                return response.getResult();
            }
            response.setIp(IPUtil.getIpAddress(request));

            StringBuffer URL = request.getRequestURL();
            String url = URL.substring(0,URL.length()-10);
            //StringBuffer URL = "http://"+request.getRemoteHost()+":"+request.getRemotePort()+"/"+request.getContextPath()+"/";

            return activityService.ActivityEnrollPay(response);
        } catch (Exception e) {
            logger.error(e.toString());
            Result rt = new Result();
            rt.setStatus(EnumConfig.S_CODE.DB_ERROR);
            return rt;
        }
    }

    /**
     * 支付完成回调
     * @param aliNotify
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "AliNotify")
    public String AliNotify(AliNotifyRes aliNotify, HttpServletRequest request) {
        try {
            //boolean signVerified = SignUtils.rsaCheckV1(paramsMap, ALIPAY_PUBLIC_KEY, "UTF-8"); //调用SDK验证签名
            //if(signVerfied){
            //  验签成功后
            //按照支付结果异步通知中的描述，对支付结果中的业务内容进行1\2\3\4二次校验，校验成功后在response中返回success，校验失败返回failure
            //}else{
            //  验签失败则记录异常日志，并在response中返回failure.
            //}
            return activityService.AliNotify(aliNotify);
        } catch (Exception e) {
            logger.error(e.toString());
            return "fail";
        }
    }

}
